﻿#region Using directives

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;

using ATA.Entities;
using ATA.Data;

#endregion

namespace ATA.Data.Bases
{	
	///<summary>
	/// This class is the base class for any <see cref="ChietKhauNhanhProviderBase"/> implementation.
	/// It exposes CRUD methods as well as selecting on index, foreign keys and custom stored procedures.
	///</summary>
	public abstract partial class ChietKhauNhanhProviderBaseCore : EntityProviderBase<ATA.Entities.ChietKhauNhanh, ATA.Entities.ChietKhauNhanhKey>
	{		
		#region Get from Many To Many Relationship Functions
		#endregion	
		
		#region Delete Methods

		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to delete.</param>
		/// <returns>Returns true if operation suceeded.</returns>
		public override bool Delete(TransactionManager transactionManager, ATA.Entities.ChietKhauNhanhKey key)
		{
			return Delete(transactionManager, key.ChietKhauNhanhId);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="chietKhauNhanhId">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public bool Delete(System.Guid chietKhauNhanhId)
		{
			return Delete(null, chietKhauNhanhId);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="chietKhauNhanhId">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public abstract bool Delete(TransactionManager transactionManager, System.Guid chietKhauNhanhId);		
		
		#endregion Delete Methods
		
		#region Get By Foreign Key Functions
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauNhanh_NhomSanPham key.
		///		FK_ChietKhauNhanh_NhomSanPham Description: 
		/// </summary>
		/// <param name="nhomSanPhamId"></param>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauNhanh objects.</returns>
		public ATA.Entities.TList<ChietKhauNhanh> GetByNhomSanPhamId(System.Guid nhomSanPhamId)
		{
			int count = -1;
			return GetByNhomSanPhamId(nhomSanPhamId, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauNhanh_NhomSanPham key.
		///		FK_ChietKhauNhanh_NhomSanPham Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="nhomSanPhamId"></param>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauNhanh objects.</returns>
		/// <remarks></remarks>
		public ATA.Entities.TList<ChietKhauNhanh> GetByNhomSanPhamId(TransactionManager transactionManager, System.Guid nhomSanPhamId)
		{
			int count = -1;
			return GetByNhomSanPhamId(transactionManager, nhomSanPhamId, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauNhanh_NhomSanPham key.
		///		FK_ChietKhauNhanh_NhomSanPham Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="nhomSanPhamId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauNhanh objects.</returns>
		public ATA.Entities.TList<ChietKhauNhanh> GetByNhomSanPhamId(TransactionManager transactionManager, System.Guid nhomSanPhamId, int start, int pageLength)
		{
			int count = -1;
			return GetByNhomSanPhamId(transactionManager, nhomSanPhamId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauNhanh_NhomSanPham key.
		///		fkChietKhauNhanhNhomSanPham Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="nhomSanPhamId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauNhanh objects.</returns>
		public ATA.Entities.TList<ChietKhauNhanh> GetByNhomSanPhamId(System.Guid nhomSanPhamId, int start, int pageLength)
		{
			int count =  -1;
			return GetByNhomSanPhamId(null, nhomSanPhamId, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauNhanh_NhomSanPham key.
		///		fkChietKhauNhanhNhomSanPham Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="nhomSanPhamId"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauNhanh objects.</returns>
		public ATA.Entities.TList<ChietKhauNhanh> GetByNhomSanPhamId(System.Guid nhomSanPhamId, int start, int pageLength,out int count)
		{
			return GetByNhomSanPhamId(null, nhomSanPhamId, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauNhanh_NhomSanPham key.
		///		FK_ChietKhauNhanh_NhomSanPham Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="nhomSanPhamId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauNhanh objects.</returns>
		public abstract ATA.Entities.TList<ChietKhauNhanh> GetByNhomSanPhamId(TransactionManager transactionManager, System.Guid nhomSanPhamId, int start, int pageLength, out int count);
		
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauNhanh_ChietKhau key.
		///		FK_ChietKhauNhanh_ChietKhau Description: 
		/// </summary>
		/// <param name="chietKhauId"></param>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauNhanh objects.</returns>
		public ATA.Entities.TList<ChietKhauNhanh> GetByChietKhauId(System.Guid chietKhauId)
		{
			int count = -1;
			return GetByChietKhauId(chietKhauId, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauNhanh_ChietKhau key.
		///		FK_ChietKhauNhanh_ChietKhau Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="chietKhauId"></param>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauNhanh objects.</returns>
		/// <remarks></remarks>
		public ATA.Entities.TList<ChietKhauNhanh> GetByChietKhauId(TransactionManager transactionManager, System.Guid chietKhauId)
		{
			int count = -1;
			return GetByChietKhauId(transactionManager, chietKhauId, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauNhanh_ChietKhau key.
		///		FK_ChietKhauNhanh_ChietKhau Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="chietKhauId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauNhanh objects.</returns>
		public ATA.Entities.TList<ChietKhauNhanh> GetByChietKhauId(TransactionManager transactionManager, System.Guid chietKhauId, int start, int pageLength)
		{
			int count = -1;
			return GetByChietKhauId(transactionManager, chietKhauId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauNhanh_ChietKhau key.
		///		fkChietKhauNhanhChietKhau Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="chietKhauId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauNhanh objects.</returns>
		public ATA.Entities.TList<ChietKhauNhanh> GetByChietKhauId(System.Guid chietKhauId, int start, int pageLength)
		{
			int count =  -1;
			return GetByChietKhauId(null, chietKhauId, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauNhanh_ChietKhau key.
		///		fkChietKhauNhanhChietKhau Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="chietKhauId"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauNhanh objects.</returns>
		public ATA.Entities.TList<ChietKhauNhanh> GetByChietKhauId(System.Guid chietKhauId, int start, int pageLength,out int count)
		{
			return GetByChietKhauId(null, chietKhauId, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhauNhanh_ChietKhau key.
		///		FK_ChietKhauNhanh_ChietKhau Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="chietKhauId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhauNhanh objects.</returns>
		public abstract ATA.Entities.TList<ChietKhauNhanh> GetByChietKhauId(TransactionManager transactionManager, System.Guid chietKhauId, int start, int pageLength, out int count);
		
		#endregion

		#region Get By Index Functions
		
		/// <summary>
		/// 	Gets a row from the DataSource based on its primary key.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to retrieve.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <returns>Returns an instance of the Entity class.</returns>
		public override ATA.Entities.ChietKhauNhanh Get(TransactionManager transactionManager, ATA.Entities.ChietKhauNhanhKey key, int start, int pageLength)
		{
			return GetByChietKhauNhanhId(transactionManager, key.ChietKhauNhanhId, start, pageLength);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the primary key PK_ChietKhauNhanh index.
		/// </summary>
		/// <param name="chietKhauNhanhId"></param>
		/// <returns>Returns an instance of the <see cref="ATA.Entities.ChietKhauNhanh"/> class.</returns>
		public ATA.Entities.ChietKhauNhanh GetByChietKhauNhanhId(System.Guid chietKhauNhanhId)
		{
			int count = -1;
			return GetByChietKhauNhanhId(null,chietKhauNhanhId, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_ChietKhauNhanh index.
		/// </summary>
		/// <param name="chietKhauNhanhId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="ATA.Entities.ChietKhauNhanh"/> class.</returns>
		public ATA.Entities.ChietKhauNhanh GetByChietKhauNhanhId(System.Guid chietKhauNhanhId, int start, int pageLength)
		{
			int count = -1;
			return GetByChietKhauNhanhId(null, chietKhauNhanhId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_ChietKhauNhanh index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="chietKhauNhanhId"></param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="ATA.Entities.ChietKhauNhanh"/> class.</returns>
		public ATA.Entities.ChietKhauNhanh GetByChietKhauNhanhId(TransactionManager transactionManager, System.Guid chietKhauNhanhId)
		{
			int count = -1;
			return GetByChietKhauNhanhId(transactionManager, chietKhauNhanhId, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_ChietKhauNhanh index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="chietKhauNhanhId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="ATA.Entities.ChietKhauNhanh"/> class.</returns>
		public ATA.Entities.ChietKhauNhanh GetByChietKhauNhanhId(TransactionManager transactionManager, System.Guid chietKhauNhanhId, int start, int pageLength)
		{
			int count = -1;
			return GetByChietKhauNhanhId(transactionManager, chietKhauNhanhId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_ChietKhauNhanh index.
		/// </summary>
		/// <param name="chietKhauNhanhId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="ATA.Entities.ChietKhauNhanh"/> class.</returns>
		public ATA.Entities.ChietKhauNhanh GetByChietKhauNhanhId(System.Guid chietKhauNhanhId, int start, int pageLength, out int count)
		{
			return GetByChietKhauNhanhId(null, chietKhauNhanhId, start, pageLength, out count);
		}
		
				
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_ChietKhauNhanh index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="chietKhauNhanhId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns an instance of the <see cref="ATA.Entities.ChietKhauNhanh"/> class.</returns>
		public abstract ATA.Entities.ChietKhauNhanh GetByChietKhauNhanhId(TransactionManager transactionManager, System.Guid chietKhauNhanhId, int start, int pageLength, out int count);
						
		#endregion "Get By Index Functions"
	
		#region Custom Methods
		
		
		#endregion

		#region Helper Functions	
		
		/// <summary>
		/// Fill a ATA.Entities.TList&lt;ChietKhauNhanh&gt; From a DataReader.
		/// </summary>
		/// <param name="reader">Datareader</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">number of rows.</param>
		/// <returns>a <see cref="ATA.Entities.TList&lt;ChietKhauNhanh&gt;"/></returns>
		public static ATA.Entities.TList<ChietKhauNhanh> Fill(IDataReader reader, ATA.Entities.TList<ChietKhauNhanh> rows, int start, int pageLength)
		{
		// advance to the starting row
		for (int i = 0; i < start; i++)
		{
			if (!reader.Read())
			return rows; // not enough rows, just return
		}
		for (int i = 0; i < pageLength; i++)
		{
			if (!reader.Read())
			break; // we are done
			string key = null;
			
			ATA.Entities.ChietKhauNhanh c = null;
			if (DataRepository.Provider.UseEntityFactory)
			{
			key = new System.Text.StringBuilder("ChietKhauNhanh")
			.Append("|").Append((reader.IsDBNull(((int)ATA.Entities.ChietKhauNhanhColumn.ChietKhauNhanhId - 1))?Guid.Empty:(System.Guid)reader[((int)ATA.Entities.ChietKhauNhanhColumn.ChietKhauNhanhId - 1)]).ToString()).ToString();
			c = EntityManager.LocateOrCreate<ChietKhauNhanh>(
			key.ToString(), // EntityTrackingKey
			"ChietKhauNhanh",  //Creational Type
			DataRepository.Provider.EntityCreationalFactoryType,  //Factory used to create entity
			DataRepository.Provider.EnableEntityTracking); // Track this entity?
			}
			else
			{
			c = new ATA.Entities.ChietKhauNhanh();
			}
			
			if (!DataRepository.Provider.EnableEntityTracking ||
			c.EntityState == EntityState.Added ||
			(DataRepository.Provider.EnableEntityTracking &&
			((DataRepository.Provider.CurrentLoadPolicy == LoadPolicy.PreserveChanges && c.EntityState == EntityState.Unchanged) ||
			(DataRepository.Provider.CurrentLoadPolicy == LoadPolicy.DiscardChanges && (c.EntityState == EntityState.Unchanged ||
								c.EntityState == EntityState.Changed)))))
						{
			c.SuppressEntityEvents = true;
			c.ChietKhauNhanhId = (System.Guid)reader["ChietKhauNhanhID"];
			c.OriginalChietKhauNhanhId = c.ChietKhauNhanhId;
			c.MucThuong = (System.Byte)reader["MucThuong"];
			c.ChietKhauId = (System.Guid)reader["ChietKhauID"];
			c.LastUpdate = (System.DateTime)reader["LastUpdate"];
			c.UpdateBy = (System.Int32)reader["UpdateBy"];
			c.NhomSanPhamId = (System.Guid)reader["NhomSanPhamID"];
			c.EntityTrackingKey = key;
			c.AcceptChanges();
			c.SuppressEntityEvents = false;
			}
			rows.Add(c);
		}
		return rows;
		}		
		/// <summary>
		/// Refreshes the <see cref="ATA.Entities.ChietKhauNhanh"/> object from the <see cref="IDataReader"/>.
		/// </summary>
		/// <param name="reader">The <see cref="IDataReader"/> to read from.</param>
		/// <param name="entity">The <see cref="ATA.Entities.ChietKhauNhanh"/> object to refresh.</param>
		public static void RefreshEntity(IDataReader reader, ATA.Entities.ChietKhauNhanh entity)
		{
			if (!reader.Read()) return;
			
			entity.ChietKhauNhanhId = (System.Guid)reader["ChietKhauNhanhID"];
			entity.OriginalChietKhauNhanhId = (System.Guid)reader["ChietKhauNhanhID"];
			entity.MucThuong = (System.Byte)reader["MucThuong"];
			entity.ChietKhauId = (System.Guid)reader["ChietKhauID"];
			entity.LastUpdate = (System.DateTime)reader["LastUpdate"];
			entity.UpdateBy = (System.Int32)reader["UpdateBy"];
			entity.NhomSanPhamId = (System.Guid)reader["NhomSanPhamID"];
			entity.AcceptChanges();
		}
		
		/// <summary>
		/// Refreshes the <see cref="ATA.Entities.ChietKhauNhanh"/> object from the <see cref="DataSet"/>.
		/// </summary>
		/// <param name="dataSet">The <see cref="DataSet"/> to read from.</param>
		/// <param name="entity">The <see cref="ATA.Entities.ChietKhauNhanh"/> object.</param>
		public static void RefreshEntity(DataSet dataSet, ATA.Entities.ChietKhauNhanh entity)
		{
			DataRow dataRow = dataSet.Tables[0].Rows[0];
			
			entity.ChietKhauNhanhId = (System.Guid)dataRow["ChietKhauNhanhID"];
			entity.OriginalChietKhauNhanhId = (System.Guid)dataRow["ChietKhauNhanhID"];
			entity.MucThuong = (System.Byte)dataRow["MucThuong"];
			entity.ChietKhauId = (System.Guid)dataRow["ChietKhauID"];
			entity.LastUpdate = (System.DateTime)dataRow["LastUpdate"];
			entity.UpdateBy = (System.Int32)dataRow["UpdateBy"];
			entity.NhomSanPhamId = (System.Guid)dataRow["NhomSanPhamID"];
			entity.AcceptChanges();
		}
		#endregion 
		
		#region DeepLoad Methods
		/// <summary>
		/// Deep Loads the <see cref="IEntity"/> object with criteria based of the child 
		/// property collections only N Levels Deep based on the <see cref="DeepLoadType"/>.
		/// </summary>
		/// <remarks>
		/// Use this method with caution as it is possible to DeepLoad with Recursion and traverse an entire object graph.
		/// </remarks>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="entity">The <see cref="ATA.Entities.ChietKhauNhanh"/> object to load.</param>
		/// <param name="deep">Boolean. A flag that indicates whether to recursively save all Property Collection that are descendants of this instance. If True, saves the complete object graph below this object. If False, saves this object only. </param>
		/// <param name="deepLoadType">DeepLoadType Enumeration to Include/Exclude object property collections from Load.</param>
		/// <param name="childTypes">ATA.Entities.ChietKhauNhanh Property Collection Type Array To Include or Exclude from Load</param>
		/// <param name="innerList">A collection of child types for easy access.</param>
	    /// <exception cref="ArgumentNullException">entity or childTypes is null.</exception>
	    /// <exception cref="ArgumentException">deepLoadType has invalid value.</exception>
		internal override void DeepLoad(TransactionManager transactionManager, ATA.Entities.ChietKhauNhanh entity, bool deep, DeepLoadType deepLoadType, System.Type[] childTypes, DeepSession innerList)
		{
			if(entity == null)
				return;

			#region NhomSanPhamIdSource	
			if (CanDeepLoad(entity, "NhomSanPham|NhomSanPhamIdSource", deepLoadType, innerList) 
				&& entity.NhomSanPhamIdSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = entity.NhomSanPhamId;
				NhomSanPham tmpEntity = EntityManager.LocateEntity<NhomSanPham>(EntityLocator.ConstructKeyFromPkItems(typeof(NhomSanPham), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.NhomSanPhamIdSource = tmpEntity;
				else
					entity.NhomSanPhamIdSource = DataRepository.NhomSanPhamProvider.GetByNhomSanPhamId(transactionManager, entity.NhomSanPhamId);		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'NhomSanPhamIdSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.NhomSanPhamIdSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.NhomSanPhamProvider.DeepLoad(transactionManager, entity.NhomSanPhamIdSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion NhomSanPhamIdSource

			#region ChietKhauIdSource	
			if (CanDeepLoad(entity, "ChietKhau|ChietKhauIdSource", deepLoadType, innerList) 
				&& entity.ChietKhauIdSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = entity.ChietKhauId;
				ChietKhau tmpEntity = EntityManager.LocateEntity<ChietKhau>(EntityLocator.ConstructKeyFromPkItems(typeof(ChietKhau), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.ChietKhauIdSource = tmpEntity;
				else
					entity.ChietKhauIdSource = DataRepository.ChietKhauProvider.GetByChietKhauId(transactionManager, entity.ChietKhauId);		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'ChietKhauIdSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.ChietKhauIdSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.ChietKhauProvider.DeepLoad(transactionManager, entity.ChietKhauIdSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion ChietKhauIdSource
			
			//used to hold DeepLoad method delegates and fire after all the local children have been loaded.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
			
			//Fire all DeepLoad Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			deepHandles = null;
		}
		
		#endregion 
		
		#region DeepSave Methods

		/// <summary>
		/// Deep Save the entire object graph of the ATA.Entities.ChietKhauNhanh object with criteria based of the child 
		/// Type property array and DeepSaveType.
		/// </summary>
		/// <param name="transactionManager">The transaction manager.</param>
		/// <param name="entity">ATA.Entities.ChietKhauNhanh instance</param>
		/// <param name="deepSaveType">DeepSaveType Enumeration to Include/Exclude object property collections from Save.</param>
		/// <param name="childTypes">ATA.Entities.ChietKhauNhanh Property Collection Type Array To Include or Exclude from Save</param>
		/// <param name="innerList">A Hashtable of child types for easy access.</param>
		internal override bool DeepSave(TransactionManager transactionManager, ATA.Entities.ChietKhauNhanh entity, DeepSaveType deepSaveType, System.Type[] childTypes, DeepSession innerList)
		{	
			if (entity == null)
				return false;
							
			#region Composite Parent Properties
			//Save Source Composite Properties, however, don't call deep save on them.  
			//So they only get saved a single level deep.
			
			#region NhomSanPhamIdSource
			if (CanDeepSave(entity, "NhomSanPham|NhomSanPhamIdSource", deepSaveType, innerList) 
				&& entity.NhomSanPhamIdSource != null)
			{
				DataRepository.NhomSanPhamProvider.Save(transactionManager, entity.NhomSanPhamIdSource);
				entity.NhomSanPhamId = entity.NhomSanPhamIdSource.NhomSanPhamId;
			}
			#endregion 
			
			#region ChietKhauIdSource
			if (CanDeepSave(entity, "ChietKhau|ChietKhauIdSource", deepSaveType, innerList) 
				&& entity.ChietKhauIdSource != null)
			{
				DataRepository.ChietKhauProvider.Save(transactionManager, entity.ChietKhauIdSource);
				entity.ChietKhauId = entity.ChietKhauIdSource.ChietKhauId;
			}
			#endregion 
			#endregion Composite Parent Properties

			// Save Root Entity through Provider
			if (!entity.IsDeleted)
				this.Save(transactionManager, entity);
			
			//used to hold DeepSave method delegates and fire after all the local children have been saved.
			Dictionary<Delegate, object> deepHandles = new Dictionary<Delegate, object>();
			//Fire all DeepSave Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			
			// Save Root Entity through Provider, if not already saved in delete mode
			if (entity.IsDeleted)
				this.Save(transactionManager, entity);
				

			deepHandles = null;
						
			return true;
		}
		#endregion
	} // end class
	
	#region ChietKhauNhanhChildEntityTypes
	
	///<summary>
	/// Enumeration used to expose the different child entity types 
	/// for child properties in <c>ATA.Entities.ChietKhauNhanh</c>
	///</summary>
	public enum ChietKhauNhanhChildEntityTypes
	{
		
		///<summary>
		/// Composite Property for <c>NhomSanPham</c> at NhomSanPhamIdSource
		///</summary>
		[ChildEntityType(typeof(NhomSanPham))]
		NhomSanPham,
			
		///<summary>
		/// Composite Property for <c>ChietKhau</c> at ChietKhauIdSource
		///</summary>
		[ChildEntityType(typeof(ChietKhau))]
		ChietKhau,
		}
	
	#endregion ChietKhauNhanhChildEntityTypes
	
	#region ChietKhauNhanhFilterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilterBuilder&lt;ChietKhauNhanhColumn&gt;"/> class
	/// that is used exclusively with a <see cref="ChietKhauNhanh"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class ChietKhauNhanhFilterBuilder : SqlFilterBuilder<ChietKhauNhanhColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the ChietKhauNhanhFilterBuilder class.
		/// </summary>
		public ChietKhauNhanhFilterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the ChietKhauNhanhFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public ChietKhauNhanhFilterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the ChietKhauNhanhFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public ChietKhauNhanhFilterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion ChietKhauNhanhFilterBuilder
	
	#region ChietKhauNhanhParameterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ParameterizedSqlFilterBuilder&lt;ChietKhauNhanhColumn&gt;"/> class
	/// that is used exclusively with a <see cref="ChietKhauNhanh"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class ChietKhauNhanhParameterBuilder : ParameterizedSqlFilterBuilder<ChietKhauNhanhColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the ChietKhauNhanhParameterBuilder class.
		/// </summary>
		public ChietKhauNhanhParameterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the ChietKhauNhanhParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public ChietKhauNhanhParameterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the ChietKhauNhanhParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public ChietKhauNhanhParameterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion ChietKhauNhanhParameterBuilder
} // end namespace
